package com.qf.commerce.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.commerce.common.dto.FreightTemplateDto;
import com.qf.commerce.common.vo.FreightTemplateVo;
import com.qf.commerce.entity.Area;
import com.qf.commerce.entity.FreightTemplate;
import com.qf.commerce.mapper.TemplateMapper;
import com.qf.commerce.service.AreaService;
import com.qf.commerce.service.TemplateService;
import com.qf.commerce.utils.CommonBeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author Administrator
 */
@Service
public class TemplateServiceImpl implements TemplateService {
    @Resource
    TemplateMapper templateMapper;
    @Resource
    AreaService areaService;

    @Override
    public int addTemplate(FreightTemplateDto freightTemplateDto) {
        FreightTemplate freightTemplate = CommonBeanUtils.copyForBean(freightTemplateDto, FreightTemplate::new);
        List<Area> areaList = freightTemplate.getArea();
        int num1 = areaList.size();
        int num2 = templateMapper.insertTemplate(freightTemplate);
        FreightTemplate f = templateMapper.selectTemplateByAdding();
        areaList.forEach((area -> {
            area.setTemplateId(f.getTemplateId());
            areaService.addArea(area, freightTemplate.getFreightSetting(), freightTemplate.getPricingManner());
        }));
        int num3 = templateMapper.updateConfirmInsert(f.getTemplateId());
        return num1 + num2 + num3;
    }

    @Override
    public int modifyTemplate(FreightTemplateDto freightTemplateDto) {
        FreightTemplate freightTemplate = CommonBeanUtils.copyForBean(freightTemplateDto, FreightTemplate::new);
        List<Area> areaList = freightTemplate.getArea();
        FreightTemplate f = templateMapper.selectTemplateById(freightTemplate.getTemplateId());
        List<Area> oldArea = f.getArea();
        int num1 = 0;
        int num2 = templateMapper.updateTemplate(freightTemplate);
        if (num2 == 0) {
            throw new RuntimeException();
        }
        if (areaList == null) {
            return num2;
        } else {
            num1=areaList.size();
            if (areaList.size()<=oldArea.size()) {
                for (Area a : areaList) {
                    for (Area b : oldArea) {
                        if(checkArea(a,b)){
                            --num1;
                        }else {
                            areaService.modifyArea(a,f.getFreightSetting(),f.getPricingManner());
                        }
                    }
                }
            }else {
                areaList.forEach(area -> {
                    if(area.getStatus()==2){
                        areaService.addArea(area,f.getFreightSetting(),f.getPricingManner());
                    }else {
                        areaService.modifyArea(area,f.getFreightSetting(),f.getPricingManner());
                    }
                });
            }
        }
        return num1 + num2;
    }


    @Override
    public int deleteTemplate(Long templateId) {
        FreightTemplate freightTemplate = templateMapper.selectTemplateById(templateId);
        List<Area> areaList = freightTemplate.getArea();
        int num1 = areaList.size();
        areaList.forEach(area -> areaService.deleteArea(area.getTemplateId()));
        int num2 = templateMapper.deleteTemplate(templateId);
        return num1 + num2;
    }

    @Override
    public PageInfo<FreightTemplateVo> queryTemplateList() {
        return CommonBeanUtils.copyPage(PageHelper.startPage(1, 10).doSelectPageInfo(() -> templateMapper.selectTemplate()),
                new PageInfo<FreightTemplateVo>(), FreightTemplateVo::new);
    }

    public boolean checkArea(Area areaA, Area areaB) {
        AtomicBoolean flag = new AtomicBoolean(true);

        if (!areaA.getAreaType().equals(areaB.getAreaType())) {
            flag.set(false);
        }
        if (!areaA.getAdditionalFreight().equals(areaB.getAdditionalFreight())) {
            flag.set(false);
        }
        if (!areaA.getAdditionalValue().equals(areaB.getAdditionalValue())) {
            flag.set(false);
        }
        if (!areaA.getBaseFreight().equals(areaB.getBaseFreight())) {
            flag.set(false);
        }
        if (areaA.getStatus().equals(areaB.getStatus())) {
            flag.set(false);
        }
        if (areaA.getAreaName().equals(areaB.getAreaName())) {
            flag.set(false);
        }
        if (!areaA.getBaseValue().equals(areaB.getBaseValue())) {
            flag.set(false);
        }

        return flag.get();
    }
}
